package com.quirka.igu;


import javax.swing.*;

import com.quirka.logica.CalcularVectorHistograma;
import com.quirka.logica.ConvertirEscalaDeGrises;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;

public class HistogramaUI extends JPanel {
  /** Cuenta las ocurrencias de cada nivel*/
  private int[] count;

  /** Set the count and display histogram */
  public void showHistogram(int[] count) {
    this.count = count;
    repaint();
  }

//  public static void main(String[] args){
//      
//      File pathImagenColor=new File("");
//      BufferedImage buffImagenGris=ConvertirEscalaDeGrises.convertir(pathImagenColor);
//      int []n=CalcularVectorHistograma.calcular(buffImagenGris);
//      
//      HistogramaUI h=new HistogramaUI();
//      JFrame f=new JFrame();
//      f.getContentPane().add(h);
//      h.showHistogram(n);
//      f.setBounds(new Rectangle(new Dimension(500, 300)));
//      f.setVisible(true);
//  }
  

  protected void paintComponent(Graphics g) {
    if (count == null) return; // No display if count is null

    super.paintComponent(g);
    super.setBackground(Color.WHITE);
    super.setAutoscrolls(true);
    // Find the panel size and bar width and interval dynamically
    int width = getWidth();
    int height = getHeight();
    int interval = (width - 40) / count.length;
    int individualWidth = (int)(((width - 40) / count.length) * 0.60);

    // Find the maximum count. The maximum count has the highest bar
    int maxCount = 0;
    for (int i = 0; i < count.length; i++) {
      if (maxCount < count[i])
        maxCount = count[i];
    }

    // x es la posicion de comienzo para la primera barra del histograma
    int x = 20;

    // dibujo la linea horizontal base
    g.drawLine(10, height - 15, width - 10, height - 15);
    
    // color de las barras    
    for (int i = 0; i < count.length; i++) {
        g.setColor(Color.blue);
      int barHeight = (int)(((double)count[i] / (double)maxCount) * (height - 30));
           
      int y = height - 15 - barHeight-1;
      
      int individualHeight = barHeight+1;
      
      g.fillRect(x, y, individualWidth, individualHeight);
      
      escribirEtiquetas(g, i, x, height);
      
      x += interval;
    }
  }
  
  private void escribirEtiquetas(Graphics g,int i, int x, int height){
      if(i==0){
          g.setColor(Color.BLACK);
          g.drawString("0", x-5 , height - 2);
      }
      if(i==50){
          g.setColor(Color.BLACK);
          g.drawString("50", x-10, height - 2);
      }
      if(i==100){
          g.setColor(Color.BLACK);
          g.drawString("100", x-10, height - 2);
      }
      if(i==150){
          g.setColor(Color.BLACK);
          g.drawString("150", x-10, height - 2);
      }
      if(i==200){
          g.setColor(Color.BLACK);
          g.drawString("200", x-10, height - 2);
      }      
      if(i==255){
          g.setColor(Color.BLACK);
          g.drawString("255", x-10, height - 2);
      }
  }

  public Dimension getPreferredSize() {
    return new Dimension(600, 200);
  }
  
  public Dimension getMinimumSize() {      
      return getPreferredSize();
    }
}
